<template>
  <div class="add">
    <el-form ref="form" :model="form" label-width="8rem">
     <el-form-item label="部门编号">
        <el-input v-model="form.depCode"></el-input>
      </el-form-item>
      <el-form-item label="部门名称">
        <el-input v-model="form.depName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveData()">保存</el-button>
        <el-button type="default" @click="back()">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import $ from "@/api/departStaff";
export default {
  data() {
    return {
      form: {
        depCodeOriginal:"",
        depCode: "",
        depName:""
      },

    };
  },
  created(){
   $.tbDmSelect({depCode:this.$route.query.id}).then(response=>{
     if(response.success)
     {
       this.form.depCodeOriginal=this.$route.query.id
       this.form.depCode=response.data.depCode
       this.form.depName=response.data.depName
     }
   })
  },
  methods: {
    saveData() {
      if (!this.validate()) return;
      $.tbDmUpdate(this.form).then(response => {
        let messageType = ""
        if (response.data == "更新成功") messageType = "success"
        else messageType = "error"
        this.$message({
          type: messageType,
          message: response.data
        });
        this.$router.replace('index')
      });
    },
    validate() {
      let error = "";
      if (this.form.depCode == "" || this.form.depCode == null) {
        error = "编号不能为空\n";
      } else if (this.form.depName == "" || this.form.depName == null) {
        error = "部门名称不能为空\n"
      }
      if (error) {
        this.$message({
          message: error,
          type: "error"
        });
        return false;
      }
      return true;
    },
    back()
    {
      this.$router.replace('index')
    }
  }
};
</script>
<style scoped>
.add {
  padding: 1rem;
}
</style>
